$('#scanQrCodePage-common').on('hidden.bs.modal', function () {
    $("#scanCode").html('');
    $("#_validNum").html('');
});

function doHideQrCodePage() {
    $('#scanQrCodePage-common').modal('hide');
}

function initScanQrCodePage(command, type, func1, func2, func3) {
    if (command == 'h5') {
        $('#scanCodeMinApp').addClass('qrCode-hidden');
        $('#scanCode').removeClass('qrCode-hidden');
    } else {
        $('#scanCodeMinApp').removeClass('qrCode-hidden');
        $('#scanCode').addClass('qrCode-hidden');
    }
    if (type == 'view') {
        $('#_passBtn').addClass('qrCode-hidden');
        $('#_unPassBtn').addClass('qrCode-hidden');
        $('#_publishBtn').addClass('qrCode-hidden');
    }
    if (type == 'check') {
        $('#_passBtn').removeClass('qrCode-hidden');
        $('#_unPassBtn').removeClass('qrCode-hidden');
        $('#_publishBtn').addClass('qrCode-hidden');
        if(func1){
            $('#_passBtn').click(function(){
                func1();
            });
        }
        if(func2){
            $('#_unPassBtn').click(function(){
                func2();
            });
        }
    }
    if (type == 'publish') {
        $('#_passBtn').addClass('qrCode-hidden');
        $('#_unPassBtn').addClass('qrCode-hidden');
        $('#_publishBtn').removeClass('qrCode-hidden');
        if(func1){
            $('#_publishBtn').click(function(){
                func1();
            });
        }
    }
    if (func3) {
        $('#_refreshQrCode').click(function(){
            func3();
        });
    }
}

function doShowQrCode(url, data, command) {
    if (!url) return;
    clearInterval(setTime);
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function(data) {
            if(!data) {
                toastr.error("二维码获取失败");
                return;
            }
            if (!data.result) {
                toastr.error("二维码获取失败，"+data.message);
                return;
            }
            var time = data.content.validNum;
            var qrCode = data.content.qrUrl;
            if (!qrCode || qrCode == '') {
                toastr.error("二维码获取失败");
                return;
            }
            initValidTime(time);
            initQrCode(qrCode, command);
            doReView(true, command);
            doSetTime();
            return;
        },
        error:function () {
            toastr.error(common_system_error);
            return;
        }
    });
}

function doRefreshQrCode(url, data, command) {
    if (!url) return;
    clearInterval(setTime);
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function(data) {
            if(!data) {
                toastr.error("二维码刷新失败");
                return;
            }
            if (!data.result) {
                toastr.error("二维码刷新失败，"+data.message);
                return;
            }
            var time = data.content.validNum;
            var qrCode = data.content.qrUrl;
            if (!qrCode || qrCode == '') {
                toastr.error("二维码刷新失败");
                return;
            }
            initValidTime(time);
            initQrCode(qrCode, command);
            doReView(null, command);
            doSetTime();
            return;
        },
        error:function () {
            toastr.error("失败！");
            return;
        }
    });
}

function initQrCode(url, command) {
    if (command == 'h5') {
        $('#scanCode').html('');
        $('#scanCode').qrcode({
            height: 240,
            width: 240,
            text: url
        });
    } else {
        $('#scanCodeMinApp').attr('src', url);
    }
}

function initValidTime(time) {
    document.getElementById("_validNum").innerHTML = time;
    document.getElementById("time_num").innerHTML = time;
}

function doReView(isShow, command) {
    if (command == 'h5') {
        $('#scanCode').removeClass('qrCode-timeout');
    } else {
        $('#scanCodeMinApp').removeClass('qrCode-timeout');
    }
    $('#scanCodeExpire').addClass('qrCode-hidden');
    if (isShow) {
        $("#scanQrCodePage-common").modal('show');
    }
}

//二维码有效期监控
var setTime = setInterval(function() {
    var time = document.getElementById("time_num").innerHTML;
    if (time) {
        if (time > 0) {
            --time;
            document.getElementById("time_num").innerHTML = time;
        }
        if (time < 1) {
            $('#scanCode').addClass('qrCode-timeout');
            $('#scanCodeMinApp').addClass('qrCode-timeout');
            $('#scanCodeExpire').removeClass('qrCode-hidden');
            clearInterval(setTime);
        }
    }
}, 1000);

function doSetTime() {
    setTime = setInterval(function() {
        var time = document.getElementById("time_num").innerHTML;
        if (time > 0) {
            --time;
            document.getElementById("time_num").innerHTML = time;
        }
        if (time < 1) {
            $('#scanCode').addClass('qrCode-timeout');
            $('#scanCodeMinApp').addClass('qrCode-timeout');
            $('#scanCodeExpire').removeClass('qrCode-hidden');
            clearInterval(setTime);
        }
    }, 1000);
}


//-------------------------------------------导入-begin--------------------------------------------------
function showImportExcelPage() {
    $('#importExcelPage').modal('show');
}

function hideImportExcelPage() {
    $('#importExcelPage').modal('hide');
    $('#_errorTipDiv').addClass('div-hidden');
    $('#_ImportExcelDiv').addClass('modal-body-height-new');
    $('#chooseFile').val('');
    $('#_fileName').html('');
    $('#_importTitle').html('');
}

function fileChangeEvent(obj) {
    var id = $(obj).attr('id');
    var f = document.getElementById(id).files;
    if (!f) {
        return true;
    }
    var name = f[0].name;
    $('#_fileName').html(name);
}

function initImportExcelPage(func1, func2, title) {
    if(func1){
        $('#_downTempBtn').click(function(){
            func1();
        });
    }
    if(func2){
        $('#_saveBtn').click(function(){
            func2();
        });
    }
    if (title) {
        $('#_importTitle').html(title);
    }
}

function doDownTempExcel(url) {
    if (!url) return;
    document.getElementById("_dataForm").action = url;
    document.getElementById("_dataForm").submit();
}

function doImportExcel(url, func) {
    if (!url) return;
    $.ajaxFileUpload({
        url : url,
        type : "post",
        secureuri : false,
        fileElementId : ["chooseFile"],
        dataType : "json",
        contentType : "application/json; charset=UTF-8",
        success : function (data) {
            if (!data) {
                toastr.error('导入失败');
                return;
            }
            if (data && !data.result) {
                toastr.error(data.message);
                return;
            }
            toastr.success('批量导入成功');
            showImportMessage(data.content);
            func();
        },
        error : function () {
            toastr.error('系统错误');
            return;
        }
    });
}

function showImportMessage(data) {
    if (!data) return;
    $('#_importTip').html(data.importTip);
    $('#errorTable').bootstrapTable('load', {
        data : data.errorData
    });
    $('#_errorTipDiv').removeClass('div-hidden');
    $('#_ImportExcelDiv').removeClass('modal-body-height-new');
}
//-------------------------------------------导入-end----------------------------------------------------

//-----------------------------判断两个时间先后顺序-------------------------------------
function judgementDate(begintimeStr,endtimeStr){
    var startTime = Date.parse(begintimeStr);
    var endTime = Date.parse(endtimeStr);
    return endTime - startTime ;
}